<template>
  <div>
    <base-header type="gradient-success" class="pb-6 pb-8 pt-5 pt-md-7"></base-header>
    <notifications></notifications>
    <div class="container-fluid mt--7">
      <div class="card shadow">
        <card type="frame">添加您每日一句内容</card>
        <div class="card-header border-0">
          <div>
            <div class="form-group row">
              <label class="col-md-2 col-form-label form-control-label">古诗标题</label>
              <div class="col-md-10">
                <textarea class="form-control" id="sentenceid"  v-model="model.sentence" rows="3" placeholder="请输入每日一句内容"></textarea>
              </div>
            </div>
           
            <div class="form-group row">
              <label
                for="example-search-input"
                class="col-md-2 col-form-label form-control-label"
              >作者</label>
              <div class="col-md-10">
                <base-input placeholder="请输入作者" v-model="model.authornick"></base-input>
              </div>
            </div>
            
            <div class="form-group row" v-show="showSaveBtn">
              <label for="example-time-input" class="col-md-2 col-form-label form-control-label"></label>
              <div class="col-md-10">
                <base-button type="success" @click="save()">
                  <i class="ni ni-send"></i>提交
                </base-button>
              </div>
            </div>
            <div class="form-group row" v-show="showUpdateBtn">
              <label for="example-time-input" class="col-md-2 col-form-label form-control-label"></label>
              <div class="col-md-10">
                <base-button type="success" @click="update()">
                  <i class="ni ni-send"></i>更新
                </base-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import validate from "~/config/utils/validate";
import { saveRjSentApi } from "~/config/api/rjeverydaysentenceservice";
import { updateRjSentApi } from "~/config/api/rjeverydaysentenceservice";
import { findRjSentByIdApi } from "~/config/api/rjeverydaysentenceservice";


export default {
  asyncData() {},
  head: {
    title: "添加诗词"
  },
  name: "tables",
  data() {
    return {
      showSaveBtn: false,
      showUpdateBtn: false,
      model: {
        id:"",
        sentence: "",
        authornick:"朗读能手官方",
        recommend:"0"
      }
    };
  },
  created: function() {
    let pageaction = this.$route.query.action;
    if (pageaction == "save") {
      this.showSaveBtn = true;
    }
    if (pageaction == "edit") {
      this.showUpdateBtn = true;
      let id=this.$route.query.id
      this.findRjSentById(id);
    }
  },
  mounted: function() {},
  methods: {
    async findRjSentById(id){
      let params={
        id:id
      }
      await findRjSentByIdApi(params).then(res => {
        if (res.sucflag) {
          this.model.sentence=res.bean.sentence,
          this.model.authornick=res.bean.authornick,
          this.model.recommend=res.bean.recommend,
          this.model.id=res.bean.id
        }
      });
    },

    async save() {
      if (!validate.required(this.model.sentence)) {
        this.$notify({
          type: "warning",
          verticalAlign: "top",
          horizontalAlign: "center",
          message: "请输入每日一句内容"
        });
        return false;
      }
      let params = {
        sentence: this.model.sentence,
        authornick: this.model.authornick,
        recommend:this.model.recommend
      };
      await saveRjSentApi(params).then(res => {
        if (res.sucflag) {
          this.$notify({
            type: "info",
            verticalAlign: "top",
            horizontalAlign: "center",
            message: "添加成功"
          });
          this.$router.push("/rjeverysentencelist");
        }
      });
    },
    async update() {
      if (!validate.required(this.model.sentence)) {
        this.$notify({
          type: "warning",
          verticalAlign: "top",
          horizontalAlign: "center",
          message: "请输入每日一句内容"
        });
        return false;
      }
      let params = {
        id:this.model.id,
        sentence: this.model.sentence,
        authornick: this.model.authornick,
        recommend:this.model.recommend
      };
      await updateRjSentApi(params).then(res => {
        if (res.sucflag) {
          this.$notify({
            type: "info",
            verticalAlign: "top",
            horizontalAlign: "center",
            message: "更新成功"
          });
          this.$router.push("/riji/rjeverysentencelist");
        }
      });
    }
  },
  layout: "DashboardLayout"
};
</script>
<style></style>
